<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<div class="row justify-content-center">
    <div class="col-md-8 text-center">
        <h1 class="display-4 mb-4">欢迎使用在线答题系统</h1>
        <p class="lead mb-4">这是一个简单的在线答题系统，你可以在这里进行答题练习。目前共有 ${totalQuestions} 道题目供你练习。</p>
        
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="card stats-card">
                    <div class="card-body">
                        <h5 class="card-title">题目总数</h5>
                        <p class="card-text">${totalQuestions}</p>
                    </div>
                </div>
            </div>
            <c:if test="${not empty sessionScope.user}">
                <div class="col-md-4">
                    <div class="card stats-card">
                        <div class="card-body">
                            <h5 class="card-title">已答题数</h5>
                            <p class="card-text" id="answeredCount">-</p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card stats-card">
                        <div class="card-body">
                            <h5 class="card-title">正确率</h5>
                            <p class="card-text" id="accuracy">-</p>
                        </div>
                    </div>
                </div>
            </c:if>
        </div>

        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body text-center">
                        <h5 class="card-title">顺序练习</h5>
                        <p class="card-text">按题目ID顺序练习</p>
                        <a href="${pageContext.request.contextPath}/question/training/sequential" class="btn btn-primary">开始练习</a>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body text-center">
                        <h5 class="card-title">随机练习</h5>
                        <p class="card-text">随机抽取题目练习</p>
                        <a href="${pageContext.request.contextPath}/question/training/random" class="btn btn-primary">开始练习</a>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body text-center">
                        <h5 class="card-title">专项练习</h5>
                        <p class="card-text">按分类专项练习</p>
                        <a href="#" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#categoryModal">开始练习</a>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card">
                    <div class="card-body text-center">
                        <h5 class="card-title">错题练习</h5>
                        <p class="card-text">练习错题本中的题目</p>
                        <a href="${pageContext.request.contextPath}/question/training/wrong_question" class="btn btn-primary">开始练习</a>
                    </div>
                </div>
            </div>
        </div>

        <!-- 分类选择模态框 -->
        <div class="modal fade" id="categoryModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">选择练习分类</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                    </div>
                    <div class="modal-body">
                        <div class="list-group" id="categoryList">
                            <!-- 分类列表将通过JavaScript动态加载 -->
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="d-grid gap-2 d-sm-flex justify-content-sm-center">
            <a href="${pageContext.request.contextPath}/question/list" class="btn btn-primary btn-lg px-4 gap-3">开始答题</a>
            <c:if test="${empty sessionScope.user}">
                <a href="${pageContext.request.contextPath}/user/register" class="btn btn-outline-secondary btn-lg px-4">注册账号</a>
            </c:if>
        </div>
    </div>
</div>

<c:if test="${not empty sessionScope.user}">
<script>
$(document).ready(function() {
    // 加载用户统计信息
    $.get('${pageContext.request.contextPath}/answer-record/api/stats', function(response) {
        if (response.code === 200) {
            const stats = response.data;
            $('#answeredCount').text(stats.totalAnswered);
            $('#accuracy').text(stats.accuracy + '%');
        }
    });

    // 加载分类列表
    $.get('${pageContext.request.contextPath}/api/category/list', function(response) {
        if (response.code === 200) {
            var categories = response.data;
            var html = '';
            categories.forEach(function(category) {
                html += '<a href="${pageContext.request.contextPath}/question/training/category?categoryId=' + 
                       category.id + '" class="list-group-item list-group-item-action">' + 
                       category.categoryName + '</a>';
            });
            $('#categoryList').html(html);
        }
    });
});
</script>
</c:if>
